<template>
  <div class="header-container">
    <!-- 顶部导航栏 -->
    <div class="top-nav">
      <div class="top-nav-inner">
        <div class="location">
          <i class="iconfont icon-location"></i>
          深圳市
        </div>
        <ul class="top-nav-list">
          <li><a href="#" class="active">顺电招聘</a></li>
          <li><a href="#" class="active">我的顺电</a></li>
          <li><a href="#">家电医院</a></li>
          <li><a href="#">为您服务</a></li>
          <li><a href="#">更多频道</a></li>
          <li><a href="#">手机顺电</a></li>
          <li><a href="#">品牌入驻</a></li>
          <li><a href="#">仓库招租</a></li>
        </ul>
      </div>
    </div>

    <!-- 中间头部区域 -->
    <div class="header-main">
      <div class="header-main-inner">
        <div class="logo">
          <a href="#" @click="index">
            <img src="/image/header/logo.png" alt="" />
          </a>
        </div>
        <div class="search-box">
          <div class="search-area">
            <input type="text" placeholder="限时专享" class="search-input" />
            <button class="search-btn">搜索</button>
          </div>
          <div class="search-links">
            <a href="#">限时专享</a> | <a href="#">戴森</a> |
            <a href="#">松下冰箱</a> | <a href="#">扫地机</a> |
            <a href="#">手机</a> | <a href="#">相机特惠</a> |
            <a href="#">Apple</a> | <a href="#">游戏本</a> |
            <a href="#">美容仪</a> |
            <a href="#">音箱</a>
          </div>
        </div>
        <div class="header-actions">
          <div v-if="userName" class="user-info" style="position: relative">
            <span class="user-name">{{ userName }}</span>
            <div class="outlogin" style="position: absolute">
              <p @click="outlogin">退出登录</p>
            </div>
          </div>
          <a v-else href="#" class="login-btn" @click="login">登录</a>
          <a v-if="userName" href="#" class="cart-btn">
            <img src="/image/header/cart.png" alt="" @click="cart" />
            <span class="cart-count">0</span>
          </a>
          <a v-else href="#" class="cart-btn">
            <img src="/image/header/cart.png" alt="" @click="login" />
            <span class="cart-count">0</span>
          </a>
        </div>
      </div>
    </div>

    <!-- 底部导航栏 -->
    <div class="bottom-nav">
      <div class="bottom-nav-inner">
        <div class="category-toggle">
          <i class="iconfont icon-menu"></i>
          所有商品分类
        </div>
        <ul class="bottom-nav-list">
          <li><a href="#">首页</a></li>
          <li><a href="#">Apple授权店</a></li>
          <li><a href="#">戴森专区</a></li>
          <li><a href="#">新品</a></li>
          <li><a href="#">样品出清</a></li>
          <li><a href="#">每周特惠</a></li>
          <li><a href="#">手机专区</a></li>
          <li><a href="#">厨电专区</a></li>
          <li><a href="#">卡萨帝专区</a></li>
          <li>
            <a href="#">摄影专区</a>
          </li>
          <li><a href="#">电脑专区</a></li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "HeaderView",
  data() {
    return {
      userName: "",
      mobile: "",
    };
  },
  created() {
    this.loadUserData();
  },
  methods: {
    index(){
      this.$router.push("/");
    },
    login() {
      this.$router.push("/login");
      console.log("登录");
    },
    outlogin() {
      localStorage.removeItem("userInfo");
      location.reload();
    },
    cart() {
      this.$router.push("/cart");
      console.log("购物车");
    },
    checkLoginStatus() {
      const userData = localStorage.getItem("userInfo");
      if (userData) {
        try {
          this.userInfo = JSON.parse(userData);
          console.log(userData);
        } catch (e) {
          console.error("解析用户数据失败:", e);
          localStorage.removeItem("userInfo");
        }
      } else {
        this.userInfo = null;
      }
    },
    loadUserData() {
      const userData = localStorage.getItem("userInfo");
      if (userData) {
        const parsed = JSON.parse(userData);
        this.userName = parsed.userName;
        this.userId = parsed.userId;
      }
    },
  },
};
</script>

<style scoped>
a {
  text-decoration: none;
}
ul {
  list-style: none;
}
.iconfont {
  font-family: "iconfont" !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* 顶部导航栏 */
.top-nav {
  background-color: #fff;
  border-bottom: 1px solid #eee;
}
.top-nav-inner {
  width: 1200px;
  height: 36px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 auto;
  padding: 0 10px;
}
.location {
  display: flex;
  align-items: center;
  color: #666;
}
.location .icon-location {
  margin-right: 5px;
  color: #0088cc;
}
.top-nav-list {
  display: flex;
}
.top-nav-list li {
  margin-left: 15px;
}
.top-nav-list li .active {
  color: #ff0000;
}

/* 中间头部区域 */
.header-main {
  background-color: #fff;
  border-bottom: 1px solid #eee;
}
.header-main-inner {
  width: 800px;
  height: 100px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 auto;
  padding: 0 10px;
}
.logo img {
  height: 40px;
}
.search-box {
  display: flex;
  flex-direction: column;
}
.search-input {
  width: 400px;
  height: 36px;
  padding: 0 10px;
  border: 1px solid #ccc;
  outline: none;
}
.search-btn {
  width: 80px;
  height: 36px;
  line-height: 36px;
  text-align: center;
  background-color: #0088cc;
  color: #fff;
  border: none;
  cursor: pointer;
  margin-top: 5px;
  align-self: flex-end;
}
.search-links {
  margin-top: 5px;
  font-size: 12px;
  color: #999;
}
.header-actions {
  display: flex;
  align-items: center;
}
.login-btn {
  width: 80px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  margin-right: 20px;
  color: #0088cc;
  border: 1px solid #000;
}
.cart-btn {
  position: relative;
  width: 40px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  border: 1px solid #000;
}
.cart-count {
  width: 20px;
  height: 20px;
  line-height: 20px;
  position: absolute;
  text-align: center;
  top: -10px;
  right: -10px;
  background-color: #ee0303;
  color: #fff;
  font-size: 10px;
  border-radius: 80%;
}

/* 底部导航栏 */
.bottom-nav {
  background-color: #333;
}
.bottom-nav-inner {
  width: 1200px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  height: 40px;
  padding: 0 10px;
}
.category-toggle {
  display: flex;
  align-items: center;
  color: #fff;
  font-size: 14px;
  margin-right: 20px;
}
.category-toggle .icon-menu {
  margin-right: 5px;
}
.bottom-nav-list {
  display: flex;
}
.bottom-nav-list li {
  margin-right: 15px;
}
.bottom-nav-list li a {
  color: #fff;
}
.user-info:hover .outlogin {
  display: block;
}
.outlogin {
  display: none;
  color: #0088cc;
  background: white;
  border: 1px solid black;
  width: 92px;
  text-align: center;
  margin-top: 10px;
}
.user-name {
  margin-right: 15px;
  padding: 9px;
  border: 1px solid gray;
}
</style>
